<template>
  <div class="body">
   <div :class="container">
    <div class="form-warp">
      <div class="sign-in-form form">
        <h2 class="form-title">欢迎家人们登录</h2>
        <!-- 用户名登录框 -->
        <input placeholder="用户名" v-model="name" @blur.capture="checkname"/>
        <span style="color:green; margin:-50px 0 0 450px;" v-if="useryz=='success'">
          <span class="el-icon-success "
          ></span>
        </span>
        <span style="color:#e60634;margin:-50px 0 0 450px" v-if="useryz=='error'">  
          <span class="el-icon-error" 
         >格式错误</span>
        </span>
        <!-- 登录密码框 -->
        <input type="password" placeholder="请输入6～16位的密码" v-model="upwd" @blur="checkupwd" />
        <span style="color:green; margin:-50px 0 0 450px;" v-if="upwdyz=='success'">
          <span class="el-icon-success "
          ></span>
        </span>
        <span style="color:#e60634;margin:-50px 0 0 450px" v-if="upwdyz=='error'">  
          <span class="el-icon-error" 
         >格式错误</span>
        </span>
        <div class="submit-btn" @click="login">立即登录</div>
      </div>
      <div class="sign-up-form form">
        <h2 class="form-title">欢迎新用户的加入</h2>
        <!-- 用户名框 -->
        <input placeholder="用户名" v-model="name" @blur.capture="checkname"/>
        <span style="color:green; margin:-50px 0 0 450px;" v-if="useryz=='success'">
          <span class="el-icon-success "
          ></span>
        </span>
        <span style="color:#e60634;margin:-50px 0 0 450px" v-if="useryz=='error'">  
          <span class="el-icon-error" 
         >格式错误</span>
        </span>
        <!-- 密码框 -->
        <input type="password" placeholder="请输入6～16位的密码" v-model="upwd" @blur="checkupwd" />
        <span style="color:green; margin:-50px 0 0 450px;" v-if="upwdyz=='success'">
          <span class="el-icon-success "
          ></span>
        </span>
        <span style="color:#e60634;margin:-50px 0 0 450px" v-if="upwdyz=='error'">  
          <span class="el-icon-error" 
         >格式错误</span>
        </span>
        <!-- 确认密码框 -->
        <input type="password" placeholder="确认密码" v-model="resupwd" @blur="checkresupwd"/>
        <span style="color:green; margin:-50px 0 0 450px;" v-if="resupwdyz=='success'">
          <span class="el-icon-success "
          ></span>
        </span>
        <span style="color:#e60634;margin:-50px 0 0 455px" v-if="resupwdyz=='error'">  
          <span class="el-icon-error" 
         >密码不一致</span>
        </span>
        <!-- 手机号框 -->
        <input type="text" placeholder="请输入手机号码" v-model="phone" @blur="checkphone"/>
        <span style="color:green; margin:-50px 0 0 450px;" v-if="phoneyz=='success'">
          <span class="el-icon-success "
          ></span>
        </span>
        <span style="color:#e60634;margin:-50px 0 0 450px" v-if="phoneyz=='error'">  
          <span class="el-icon-error" 
         >格式错误</span>
        </span>
        <div class="submit-btn" @click="register">立即注册</div>
      </div>
    </div>
    <div class="desc-warp">
      <div class="desc-warp-item sign-up-desc">
        <div class="content">
          <router-link to="/">
            <button>回到首页</button>
            </router-link>
          <button id="sign-up-btn" @click="singUpBtn">注册</button>
        </div>
        <img src="/img/Login/log.svg" alt="">
      </div>
      <div class="desc-warp-item sign-in-desc">
        <div class="content">
          <button id="sign-in-btn" @click="singInBtn">登录</button>
          <router-link to="/">
            <button>回到首页</button>
            </router-link>
        </div>
        <img src="/img/Login/register.svg" alt="">
      </div>
    </div>
  </div>
  </div>
</template>
<script>
import {mapMutations,mapActions} from 'vuex'
export default {
  data(){
    return{
      container:'container',
      resp:{},
      useryz:'',
      upwdyz:'',
      resupwdyz:'',
      phoneyz:'',
      // 注册
      name:'',
      upwd:'',
      resupwd:'',
      phone:''
    }
  },
  methods:{
    ...mapMutations(["setname"]),
    singUpBtn(){
      this.container='container sign-up-mode'
    },
    singInBtn(){
      this.container='container'
    },
    // 注册
    checkname(){
      let reg=/^[\u4e00-\u9fa5_a-zA-Z0-9-]{1,16}$/;
      if(reg.test(this.name)){
        this.useryz='success'
      }else{
        this.useryz='error'
      }
    },
    checkupwd(){
      let reg=/^[0-9A-Za-z]{6,16}$/;
      if(reg.test(this.upwd)){
        this.upwdyz='success'
      }else{
        this.upwdyz='error'
      }
    },
    checkresupwd(){
      if(this.upwd==this.resupwd&&this.resupwd!=''){
        this.resupwdyz='success'
      }else{
        this.resupwdyz='error'
      }
    },
    checkphone(){
      let reg=/^1[3-9][0-9]{9}$/;
      if(reg.test(this.phone)){
        this.phoneyz='success'
      }else{
        this.phoneyz='error'
      }
    },
    register(){
      if(this.useryz=='success'&&this.upwdyz=='success'&&this.phoneyz=='success'&&this.resupwdyz=='success'){
        this.axios.post('/register',`name=${this.name}&&upwd=${this.upwd}&&phone=${this.phone}`).then(result=>{
          console.log(result)
          if(result.data.code==201){
            this.$alert('注册失败','提示',{type:'error'})
            this.$router.push('/register')
          }else{
            this.container='container';
          }
        })
      }
  },
  login(){
    if(this.useryz=='success'&&this.upwdyz=='success'){
        this.axios.post('/login',`name=${this.name}&&upwd=${this.upwd}`).then(result=>{
          console.log(result)
          if(result.data.code==201){
            this.$alert('账号或密码不正确','提示',{type:'error'})
          }else{
            console.log(result.data.result.name)
            //将用户名放入sessionStorage
            sessionStorage.setItem("user",result.data.result.name)
            //将用户名放入vuex
            this.$store.commit("setname",result.data.result.name);
            console.log(result.data.result.name)
            //打印login状态
            this.$alert('登录成功','提示',{type:'success'})
            this.$router.push('/')
          }
        })
      }
  }
  },
}
</script>
<style scoped>
.body{
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  color: #333;
}

.container {
  position: relative;
  min-height: 100vh;
  width: 100%;
  overflow: hidden;
}
.container::before {
  content: " ";
  position: absolute;
  width: 2000px;
  height: 2000px;
  border-radius: 50%;
  background-image: linear-gradient(-45deg, #6266f5 0%, #04befe 100%);
  transition: 1.8s ease-in-out;
  z-index: 6;
  top: -10%;
  right: 48%;
  transform: translateY(-50%);
}
.container.sign-up-mode::before {
  transform: translate(100%, -50%);
}

.form-warp {
  width: 50%;
  position: absolute;
  z-index: 5;
  left: 75%;
  top: 50%;
  z-index: 5;
  transform: translate(-50%, -50%);
  display: grid;
  grid-template-columns: 1fr;
  transition: 1s 0.7s ease-in-out; 
}
.form-warp .form {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 25px;
  /* 将两个 form 布局在 grid 同一位置 */
  grid-row: 1 / 2;
  grid-column: 1 / 2;
  transition: all 0.2s 0.7s;
  opacity: 1;
  z-index: 4;
}
.form-title {
  color: #6266f5;
}
.form-warp .sign-up-form {
  opacity: 0;
  z-index: 3;
}
.container.sign-up-mode .form-warp {
  left: 25%;
}
.container.sign-up-mode .sign-in-form {
  opacity: 0;
  z-index: 3;
}
.container.sign-up-mode .sign-up-form {
  opacity: 1;
  z-index: 4;
}
input,
.submit-btn {
  min-width: 300px;
  outline: none;
  padding: 12px 30px;
  line-height: 1;
  font-size: 16px;
  border-radius: 60px;
  color: #333;
  background-color: #6267f513;
  border: none;
}
input::placeholder {
  color: #cccc;
}
.submit-btn {
  background-color: #6266f5;
  color: #FFF;
  text-align: center;
  min-width: 150px;
  font-size: initial;
  font-weight: bold;
  letter-spacing: 1.5px;
  cursor: pointer;
}

.desc-warp {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}
.desc-warp-item {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-around;
  text-align: center;
  text-align: center;
  padding: 3rem 17% 2rem 12%;
  z-index: 6;
}
/* 事件穿透 BEGIN */
.sign-in-desc {
  pointer-events: none;
}
.sign-up-mode .sign-in-desc {
  pointer-events: all;
}
.sign-up-mode .sign-up-desc {
  pointer-events: none;
}
/* 事件穿透 END */
.content {
  width: 100%;
  transition: transform 0.9s ease-in-out;
  transition-delay: .6s;
}
.sign-in-desc img,
.sign-in-desc .content {
  transform: translateX(800px);
}
.sign-up-mode .sign-in-desc img,
.sign-up-mode .sign-in-desc .content {
  transform: translateX(0);
}

.sign-up-mode .sign-up-desc img,
.sign-up-mode .sign-up-desc .content {
  transform: translateX(-800px);
}

button {
  outline: none;
  padding: 6px 8px;
  min-width: 100px;
  text-align: center;
  border-radius: 30px;
  border: 2px solid #FFF;
  background: none;
  color: #FFF;
  cursor: pointer;
  transition: all .3s ease;
}
button:active {
  background: rgba(255, 255, 255, .1);
}
img {
  width: 100%;
  display: block;
  transition: transform 0.9s ease-in-out;
  transition-delay: .5s;
}

/* 响应式 */
@media screen and (max-width: 870px) {
  .container::before {
    width: 1500px;
    height: 1500px;
    transform: translateX(-50%);
    left: 16%;
    bottom: 68%;
    right: initial;
    top: initial;
    transition: 2s ease-in-out;
  }
  .container.sign-up-mode::before {
    transform: translate(-50%, 100%);
    bottom: 32%;
    right: initial;
  }
  .form-warp {
    width: 100%;
    top: 75%;
    left: 50%;
    transform: translate(-50%, -100%);
    transition: 1s 0.8s ease-in-out;
  }
  .container.sign-up-mode .form-warp {
    top: 25%;
    left: 50%;
    transform: translate(-50%, 0);
  }
  img {
    width: 200px;
    transition: transform 0.9s ease-in-out;
    transition-delay: 0.7s;
  }
  .desc-warp {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 2fr 1fr;
  }
  .desc-warp-item {
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    padding: 2.5rem 8%;
    grid-column: 1 / 2;
  }
  .sign-in-desc {
    grid-row: 3 / 4;
  }

  .sign-in-desc img,
  .sign-in-desc .content {
    transform: translateY(800px);
  }

  .sign-up-mode .sign-in-desc img,
  .sign-up-mode .sign-in-desc .content {
    transform: translateY(0);
  }

  .sign-up-mode .sign-up-desc img,
  .sign-up-mode .sign-up-desc .content {
    transform: translateY(-800px);
  }
}

@media screen and (max-width: 570px) {
  .container::before {
    bottom: 72%;
    left: 50%;
  }
  img {
    display: none;
  }
}
</style>